/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.web.investorfinancialinfo;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.base.entity.investorfinancialinfo.*;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.service.investorfinancialinfo.InvestorFinancialInfoService;
import com.jeesite.modules.base.service.supplier.SupplierService;
import com.jeesite.modules.custom.enumInfo.InvestorFinanceInfoEnum;
import com.jeesite.modules.custom.enumInfo.supplierTypeEnum;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 资方金融费率维护Controller
 *
 * @author LV
 * @version 2021-03-03
 */
@Controller
@RequestMapping(value = "${adminPath}/base/investorfinancialinfo/investorFinancialInfo")
public class InvestorFinancialInfoController extends BaseController {

    @Autowired
    private InvestorFinancialInfoService investorFinancialInfoService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private UserService userService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public InvestorFinancialInfo get(String investorFinancialInfoId, boolean isNewRecord) {
        return investorFinancialInfoService.get(investorFinancialInfoId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = {"list", ""})
    public String list(InvestorFinancialInfo investorFinancialInfo, Model model) {
        Supplier supplier = new Supplier();
        supplier.setSupplierType("1");// 国内资方
        List < Supplier > listSupplier = supplierService.findList(supplier);
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page < InvestorFinancialInfo > listData(InvestorFinancialInfo investorFinancialInfo, HttpServletRequest request, HttpServletResponse response) {
        investorFinancialInfo.setPage(new Page <>(request, response));
        Page < InvestorFinancialInfo > page = investorFinancialInfoService.findPage(investorFinancialInfo);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "form")
    public String form(InvestorFinancialInfo investorFinancialInfo, Model model) {
        if (!investorFinancialInfo.getIsNewRecord()) {
            // 金融信息-押汇利息-资方阶段 1,客户阶段 2
            List < NegotiatedInterest > listNego = investorFinancialInfo.getNegotiatedInterestList();
            List < NegotiatedInterest > listNego1 = ListUtils.newArrayList();
            List < NegotiatedInterest > listNego2 = ListUtils.newArrayList();
            for (NegotiatedInterest negotiatedInterest : listNego) {
                if ("1".equals(negotiatedInterest.getStageType())) {
                    listNego1.add(negotiatedInterest);
                } else if ("2".equals(negotiatedInterest.getStageType())) {
                    listNego2.add(negotiatedInterest);
                }
            }
            investorFinancialInfo.setNegotiatedInterestList1(listNego1);
            investorFinancialInfo.setNegotiatedInterestList2(listNego2);

            // 金融信息-垫税利息-关税保证保险1,现金2,舶来车3,国内融资4
            List < PadTaxInterest > listPad = investorFinancialInfo.getPadTaxInterestList();
            List < PadTaxInterest > listPad1 = ListUtils.newArrayList();
            List < PadTaxInterest > listPad2 = ListUtils.newArrayList();
            List < PadTaxInterest > listPad3 = ListUtils.newArrayList();
            List < PadTaxInterest > listPad4 = ListUtils.newArrayList();
            for (PadTaxInterest padTaxInterest : listPad) {
                if ("1".equals(padTaxInterest.getPadTaxInterestType())) {
                    listPad1.add(padTaxInterest);
                } else if ("2".equals(padTaxInterest.getPadTaxInterestType())) {
                    listPad2.add(padTaxInterest);
                } else if ("3".equals(padTaxInterest.getPadTaxInterestType())) {
                    listPad3.add(padTaxInterest);
                } else if ("4".equals(padTaxInterest.getPadTaxInterestType())) {
                    listPad4.add(padTaxInterest);
                }
            }
            investorFinancialInfo.setPadTaxInterestList1(listPad1);
            investorFinancialInfo.setPadTaxInterestList2(listPad2);
            investorFinancialInfo.setPadTaxInterestList3(listPad3);
            investorFinancialInfo.setPadTaxInterestList4(listPad4);
        }
        Supplier supplier = new Supplier();
        supplier.setSupplierType("1");// 国内资方
        List < Supplier > listSupplier = supplierService.findList(supplier);
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoForm";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "detail")
    public String detail(InvestorFinancialInfo investorFinancialInfo, Model model) {
        // 金融信息-押汇利息-资方阶段 1,客户阶段 2
        List < NegotiatedInterest > listNego = investorFinancialInfo.getNegotiatedInterestList();
        List < NegotiatedInterest > listNego1 = ListUtils.newArrayList();
        List < NegotiatedInterest > listNego2 = ListUtils.newArrayList();
        for (NegotiatedInterest negotiatedInterest : listNego) {
            if ("1".equals(negotiatedInterest.getStageType())) {
                listNego1.add(negotiatedInterest);
            } else if ("2".equals(negotiatedInterest.getStageType())) {
                listNego2.add(negotiatedInterest);
            }
        }
        investorFinancialInfo.setNegotiatedInterestList1(listNego1);
        investorFinancialInfo.setNegotiatedInterestList2(listNego2);

        // 金融信息-垫税利息-关税保证保险1,现金2,舶来车3,国内融资4
        List < PadTaxInterest > listPad = investorFinancialInfo.getPadTaxInterestList();
        List < PadTaxInterest > listPad1 = ListUtils.newArrayList();
        List < PadTaxInterest > listPad2 = ListUtils.newArrayList();
        List < PadTaxInterest > listPad3 = ListUtils.newArrayList();
        List < PadTaxInterest > listPad4 = ListUtils.newArrayList();
        for (PadTaxInterest padTaxInterest : listPad) {
            if ("1".equals(padTaxInterest.getPadTaxInterestType())) {
                listPad1.add(padTaxInterest);
            } else if ("2".equals(padTaxInterest.getPadTaxInterestType())) {
                listPad2.add(padTaxInterest);
            } else if ("3".equals(padTaxInterest.getPadTaxInterestType())) {
                listPad3.add(padTaxInterest);
            } else if ("4".equals(padTaxInterest.getPadTaxInterestType())) {
                listPad4.add(padTaxInterest);
            }
        }
        investorFinancialInfo.setPadTaxInterestList1(listPad1);
        investorFinancialInfo.setPadTaxInterestList2(listPad2);
        investorFinancialInfo.setPadTaxInterestList3(listPad3);
        investorFinancialInfo.setPadTaxInterestList4(listPad4);

        Supplier supplier = new Supplier();
        supplier.setSupplierType("1");// 国内资方
        List < Supplier > listSupplier = supplierService.findList(supplier);
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoDetail";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:edit")
    @PostMapping(value = "save")
    @ResponseBody
    @Transactional(readOnly = false)
    public String save(@Validated InvestorFinancialInfo investorFinancialInfo) {
        InvestorFinancialInfo investorFinancialInfo1 = new InvestorFinancialInfo();
        Supplier supplier = new Supplier();
        supplier.setSupplierId(investorFinancialInfo.getSupplier().getSupplierId());
        investorFinancialInfo1.setSupplier(supplier);

        if (investorFinancialInfo.getIsNewRecord()) {
            List < InvestorFinancialInfo > listInfo = investorFinancialInfoService.findList(investorFinancialInfo1);
            if (listInfo != null && !listInfo.isEmpty()) {
                return renderResult(Global.FALSE, text("同一资方不能有多个该档案记录！"));
            }
            investorFinancialInfo.setInvestorFinancialInfoId(codeRuleService.getCode("financialrate"));
            investorFinancialInfo.setCreateBy(UserUtils.getUser().getUserCode());
            investorFinancialInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        } else {
            InvestorFinancialInfo oldInvestorFinancialInfo = investorFinancialInfoService.getOneByID(investorFinancialInfo.getInvestorFinancialInfoId());
            if (!investorFinancialInfo.getSupplier().getSupplierId().equals(oldInvestorFinancialInfo.getSupplier().getSupplierId())) {
                List < InvestorFinancialInfo > listInfo = investorFinancialInfoService.findList(investorFinancialInfo1);
                if (listInfo != null && !listInfo.isEmpty()) {
                    return renderResult(Global.FALSE, text("同一资方不能有多个该档案记录！"));
                }
            }
            investorFinancialInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        }

        // 选项名称赋值
        List < LcFinancialRatio > lcFinancialRatioList = investorFinancialInfo.getLcFinancialRatioList();
        for (LcFinancialRatio lc : lcFinancialRatioList) {
            lc.setOptionsName(lc.getMarginRatio() + "%+" + lc.getPrepaidExpenseRatio() + "%+" + lc.getNegotiatedRatio() + "%");
        }
        List < TtFinancialRatio > ttFinancialRatioList = investorFinancialInfo.getTtFinancialRatioList();
        for (TtFinancialRatio tt : ttFinancialRatioList) {
            tt.setOptionsName(tt.getMarginRatio() + "%+" + tt.getPrepaidExpenseRatio() + "%");
        }

        // 金融信息-押汇利息-资方阶段 1,客户阶段 2
        List < NegotiatedInterest > listNego1 = investorFinancialInfo.getNegotiatedInterestList1();
        List < NegotiatedInterest > listNego2 = investorFinancialInfo.getNegotiatedInterestList2();
        List < NegotiatedInterest > listNego = ListUtils.newArrayList();
        listNego.addAll(listNego1);
        listNego.addAll(listNego2);
        investorFinancialInfo.setNegotiatedInterestList(listNego);

        // 金融信息-垫税利息-关税保证保险1,现金2,舶来车3,国内融资4
        List < PadTaxInterest > listPad = investorFinancialInfo.getPadTaxInterestList();
        List < PadTaxInterest > listPad1 = investorFinancialInfo.getPadTaxInterestList1();
        if (!CheckPadTax(listPad1)) {
            return renderResult(Global.FALSE, text("金融信息-垫税利息-关税保证保险的开始、结束天数必须连续！"));
        }
        List < PadTaxInterest > listPad2 = investorFinancialInfo.getPadTaxInterestList2();
        if (!CheckPadTax(listPad2)) {
            return renderResult(Global.FALSE, text("金融信息-垫税利息-现金保函的开始、结束天数必须连续！"));
        }
        List < PadTaxInterest > listPad3 = investorFinancialInfo.getPadTaxInterestList3();
        if (!CheckPadTax(listPad3)) {
            return renderResult(Global.FALSE, text("金融信息-垫税利息-舶来车的开始、结束天数必须连续！"));
        }
        List < PadTaxInterest > listPad4 = investorFinancialInfo.getPadTaxInterestList4();
        if (!CheckPadTax(listPad1)) {
            return renderResult(Global.FALSE, text("国内融资信息的开始、结束天数必须连续！"));
        }
        listPad.addAll(listPad1);
        listPad.addAll(listPad2);
        listPad.addAll(listPad3);
        listPad.addAll(listPad4);
        investorFinancialInfo.setPadTaxInterestList(listPad);

        investorFinancialInfoService.save(investorFinancialInfo);
        if (investorFinancialInfo.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("financialrate");
        }
        return renderResult(Global.TRUE, text("保存资方金融费率维护成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:delete")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(InvestorFinancialInfo investorFinancialInfo) {
        investorFinancialInfoService.delete(investorFinancialInfo);
        return renderResult(Global.TRUE, text("删除资方金融费率维护成功！"));
    }


/***************************************************************************************************************************************************/

    /**
     * 检查开始、结束天数是否连续
     *
     * @param list
     * @return
     */
    public boolean CheckPadTax(List < PadTaxInterest > list) {
        Long old = 0L;
//        String groupName;
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                if (list.get(0).getStartDays() != 0) {
                    return false;
                }
                continue;
            } else {
                old = list.get(i - 1).getEndDays();
//                groupName = list.get(i - 1).getGroupName();
            }
            /*if ((list.get(i).getGroupName().equals(groupName) && list.get(i).getStartDays() != (old + 1)) ||
                    (!list.get(i).getGroupName().equals(groupName) && list.get(i).getStartDays() != 0)) {
                return false;
            }*/
            if (list.get(i).getStartDays() == 0
                    || list.get(i).getStartDays() >= list.get(i).getEndDays()
                    || (list.get(i).getStartDays() != (old + 1))) {
                return false;
            }
        }
        return true;
    }

/***************************************************************************************************************************************************/


    /**
     * 查询列表
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = {"listTTLC", ""})
    public String listTTLC(InvestorFinancialInfo investorFinancialInfo, Model model) {
        Supplier supplier = new Supplier();
        supplier.setSupplierType(supplierTypeEnum.IN_SUPPLIER.getCode());// 国内资方
        List < Supplier > listSupplier = supplierService.findList(supplier);
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoListTTLC";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "listDataTTLC")
    @ResponseBody
    public Page < InvestorFinancialInfo > listDataTTLC(InvestorFinancialInfo investorFinancialInfo, HttpServletRequest request, HttpServletResponse response) {
        investorFinancialInfo.setPage(new Page <>(request, response));
        investorFinancialInfo.setFinancingType(InvestorFinanceInfoEnum.BASETTLC.getCode()); // TTLC
        Page < InvestorFinancialInfo > page = investorFinancialInfoService.findPage(investorFinancialInfo);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "formTTLC")
    public String formTTLC(InvestorFinancialInfo investorFinancialInfo, Model model) {
        Supplier supplier = new Supplier();
        supplier.setSupplierType(supplierTypeEnum.IN_SUPPLIER.getCode());// 国内资方
        List < Supplier > listSupplier = supplierService.findList(supplier);
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoFormTTLC";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "detailTTLC")
    public String detailTTLC(InvestorFinancialInfo investorFinancialInfo, Model model) {
        Supplier supplier = new Supplier();
        supplier.setSupplierType(supplierTypeEnum.IN_SUPPLIER.getCode());// 国内资方
        List < Supplier > listSupplier = supplierService.findList(supplier);
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoDetailTTLC";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:edit")
    @PostMapping(value = "saveTTLC")
    @ResponseBody
    @Transactional(readOnly = false)
    public String saveTTLC(@Validated InvestorFinancialInfo investorFinancialInfo) {
        InvestorFinancialInfo investorFinancialInfo1 = new InvestorFinancialInfo();
        investorFinancialInfo1.setInvestorName(investorFinancialInfo.getInvestorName());
        if (investorFinancialInfo.getIsNewRecord()) {
            List < InvestorFinancialInfo > listInfo = investorFinancialInfoService.findList(investorFinancialInfo1);
            if (listInfo != null && !listInfo.isEmpty()) {
                return renderResult(Global.FALSE, text("同一资方不能有多个该档案记录！"));
            }
            investorFinancialInfo.setInvestorFinancialInfoId(codeRuleService.getCode("financialrate"));
            investorFinancialInfo.setCreateBy(UserUtils.getUser().getUserCode());
            investorFinancialInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        } else {
            InvestorFinancialInfo oldInvestorFinancialInfo = investorFinancialInfoService.getOneByID(investorFinancialInfo.getInvestorFinancialInfoId());
            if (!investorFinancialInfo.getInvestorName().equals(oldInvestorFinancialInfo.getInvestorName())) {
                List < InvestorFinancialInfo > listInfo = investorFinancialInfoService.findList(investorFinancialInfo1);
                if (listInfo != null && !listInfo.isEmpty()) {
                    return renderResult(Global.FALSE, text("同一资方不能有多个该档案记录！"));
                }
            }
            investorFinancialInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        }
        // 选项名称赋值
        List < LcFinancialRatio > lcFinancialRatioList = investorFinancialInfo.getLcFinancialRatioList();
        for (LcFinancialRatio lc : lcFinancialRatioList) {
            lc.setOptionsName(lc.getMarginRatio() + "%+" + lc.getPrepaidExpenseRatio() + "%+" + lc.getNegotiatedRatio() + "%");
        }
        List < TtFinancialRatio > ttFinancialRatioList = investorFinancialInfo.getTtFinancialRatioList();
        for (TtFinancialRatio tt : ttFinancialRatioList) {
            tt.setOptionsName(tt.getMarginRatio() + "%+" + tt.getPrepaidExpenseRatio() + "%");
        }

        investorFinancialInfoService.save(investorFinancialInfo);
        if (investorFinancialInfo.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("financialrate");
        }
        return renderResult(Global.TRUE, text("保存资方金融费率维护成功！"));
    }


    /**
     * 查询列表
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = {"listNegotiate", ""})
    public String listNegotiate(InvestorFinancialInfo investorFinancialInfo, Model model) {
        List < Supplier > list = ListUtils.newArrayList();
        for (InvestorFinancialInfo info : investorFinancialInfoService.findList(new InvestorFinancialInfo())) {
            Supplier supplier = new Supplier();
            supplier.setSupplierId(info.getSupplier().getId());
            supplier.setSupplierName(info.getSupplier().getSupplierName());
            list.add(supplier);
        }
        List < String > names = ListUtils.newArrayList();
        List < Supplier > listSupplier = list.stream().filter(// 过滤去重
                v -> {
                    boolean flag = !names.contains(v.getSupplierId());
                    names.add(v.getSupplierId());
                    return flag;
                }
        ).collect(Collectors.toList());
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoListNegotiate";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "listDataNegotiate")
    @ResponseBody
    public Page < InvestorFinancialInfo > listDataNegotiate(InvestorFinancialInfo investorFinancialInfo, HttpServletRequest request, HttpServletResponse response) {
        investorFinancialInfo.setPage(new Page <>(request, response));
        investorFinancialInfo.setFinancingType(InvestorFinanceInfoEnum.NEGOTIATE.getCode()); // 押汇
        Page < InvestorFinancialInfo > page = investorFinancialInfoService.findPage(investorFinancialInfo);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "formNegotiate")
    public String formNegotiate(InvestorFinancialInfo investorFinancialInfo, Model model) {
        if (!investorFinancialInfo.getIsNewRecord()) {
            // 金融信息-押汇利息-资方阶段 1,客户阶段 2
            List < NegotiatedInterest > listNego = investorFinancialInfo.getNegotiatedInterestList();
            List < NegotiatedInterest > listNego1 = ListUtils.newArrayList();
            List < NegotiatedInterest > listNego2 = ListUtils.newArrayList();
            for (NegotiatedInterest negotiatedInterest : listNego) {
                if ("1".equals(negotiatedInterest.getStageType())) {
                    listNego1.add(negotiatedInterest);
                } else if ("2".equals(negotiatedInterest.getStageType())) {
                    listNego2.add(negotiatedInterest);
                }
            }
            investorFinancialInfo.setNegotiatedInterestList1(listNego1);
            investorFinancialInfo.setNegotiatedInterestList2(listNego2);
        }
        List < Supplier > list = ListUtils.newArrayList();
        for (InvestorFinancialInfo info : investorFinancialInfoService.findList(new InvestorFinancialInfo())) {
            Supplier supplier = new Supplier();
            supplier.setSupplierId(info.getSupplier().getId());
            supplier.setSupplierName(info.getSupplier().getSupplierName());
            list.add(supplier);
        }
        List < String > names = ListUtils.newArrayList();
        List < Supplier > listSupplier = list.stream().filter(// 过滤去重
                v -> {
                    boolean flag = !names.contains(v.getSupplierId());
                    names.add(v.getSupplierId());
                    return flag;
                }
        ).collect(Collectors.toList());

        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoFormNegotiate";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "detailNegotiate")
    public String detailNegotiate(InvestorFinancialInfo investorFinancialInfo, Model model) {
        // 金融信息-押汇利息-资方阶段 1,客户阶段 2
        List < NegotiatedInterest > listNego = investorFinancialInfo.getNegotiatedInterestList();
        List < NegotiatedInterest > listNego1 = ListUtils.newArrayList();
        List < NegotiatedInterest > listNego2 = ListUtils.newArrayList();
        for (NegotiatedInterest negotiatedInterest : listNego) {
            if ("1".equals(negotiatedInterest.getStageType())) {
                listNego1.add(negotiatedInterest);
            } else if ("2".equals(negotiatedInterest.getStageType())) {
                listNego2.add(negotiatedInterest);
            }
        }
        investorFinancialInfo.setNegotiatedInterestList1(listNego1);
        investorFinancialInfo.setNegotiatedInterestList2(listNego2);

        Supplier supplier = new Supplier();
        supplier.setSupplierType(supplierTypeEnum.IN_SUPPLIER.getCode());// 国内资方
        List < Supplier > listSupplier = supplierService.findList(supplier);
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoDetailNegotiate";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:edit")
    @PostMapping(value = "saveNegotiate")
    @ResponseBody
    @Transactional(readOnly = false)
    public String saveNegotiate(@Validated InvestorFinancialInfo investorFinancialInfo) {
        InvestorFinancialInfo investorFinancialInfo1 = new InvestorFinancialInfo();
        investorFinancialInfo1.setGroupName(investorFinancialInfo.getGroupName());

        if (investorFinancialInfo.getIsNewRecord()) {
            List < InvestorFinancialInfo > listInfo = investorFinancialInfoService.findList(investorFinancialInfo1);
            if (listInfo != null && !listInfo.isEmpty()) {
                return renderResult(Global.FALSE, text("方案名称已存在！"));
            }
            investorFinancialInfo.setInvestorFinancialInfoId(codeRuleService.getCode("financialrateyh"));
            investorFinancialInfo.setCreateBy(UserUtils.getUser().getUserCode());
            investorFinancialInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        } else {
            InvestorFinancialInfo oldInvestorFinancialInfo = investorFinancialInfoService.getOneByID(investorFinancialInfo.getInvestorFinancialInfoId());
            if (!investorFinancialInfo.getGroupName().equals(oldInvestorFinancialInfo.getGroupName())) {
                List < InvestorFinancialInfo > listInfo = investorFinancialInfoService.findList(investorFinancialInfo1);
                if (listInfo != null && !listInfo.isEmpty()) {
                    return renderResult(Global.FALSE, text("方案名称已存在！"));
                }
            }
            investorFinancialInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        }

        // 金融信息-押汇利息-资方阶段 1,客户阶段 2
        List < NegotiatedInterest > listNego1 = investorFinancialInfo.getNegotiatedInterestList1();
        List < NegotiatedInterest > listNego2 = investorFinancialInfo.getNegotiatedInterestList2();

        // 阶段赋值
        int supCode = 0;
        int CusCode = 0;
        for (NegotiatedInterest negotiatedInterest : listNego1) {
            if (!NegotiatedInterest.STATUS_DELETE.equals(negotiatedInterest.getStatus())) {
                negotiatedInterest.setStage(supCode);
                supCode++;
            }
        }
        for (NegotiatedInterest negotiatedInterest : listNego2) {
            if (!NegotiatedInterest.STATUS_DELETE.equals(negotiatedInterest.getStatus())) {
                negotiatedInterest.setStage(CusCode);
                CusCode++;
            }
        }
        List < NegotiatedInterest > listNego = ListUtils.newArrayList();
        listNego.addAll(listNego1);
        listNego.addAll(listNego2);
        investorFinancialInfo.setNegotiatedInterestList(listNego);

        investorFinancialInfoService.save(investorFinancialInfo);
        if (investorFinancialInfo.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("financialrateyh");
        }
        return renderResult(Global.TRUE, text("保存资方金融费率维护成功！"));
    }


    /**
     * 查询列表
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = {"listPadTax", ""})
    public String listPadTax(InvestorFinancialInfo investorFinancialInfo, Model model) {
        List < Supplier > list = ListUtils.newArrayList();
        for (InvestorFinancialInfo info : investorFinancialInfoService.findList(new InvestorFinancialInfo())) {
            Supplier supplier = new Supplier();
            supplier.setSupplierId(info.getSupplier().getId());
            supplier.setSupplierName(info.getSupplier().getSupplierName());
            list.add(supplier);
        }
        List < String > names = ListUtils.newArrayList();
        List < Supplier > listSupplier = list.stream().filter(// 过滤去重
                v -> {
                    boolean flag = !names.contains(v.getSupplierId());
                    names.add(v.getSupplierId());
                    return flag;
                }
        ).collect(Collectors.toList());
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoListPadTax";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "listDataPadTax")
    @ResponseBody
    public Page < InvestorFinancialInfo > listDataPadTax(InvestorFinancialInfo investorFinancialInfo, HttpServletRequest request, HttpServletResponse response) {
        investorFinancialInfo.setPage(new Page <>(request, response));
        investorFinancialInfo.setFinancingType(InvestorFinanceInfoEnum.PADTAX.getCode()); // 垫税
        Page < InvestorFinancialInfo > page = investorFinancialInfoService.findPage(investorFinancialInfo);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "formPadTax")
    public String formPadTax(InvestorFinancialInfo investorFinancialInfo, Model model) {
        if (!investorFinancialInfo.getIsNewRecord()) {
            // 金融信息-垫税利息-关税保证保险1,现金2,保函3，舶来车4
            List < PadTaxInterest > listPad = investorFinancialInfo.getPadTaxInterestList();
            List < PadTaxInterest > listPad1 = ListUtils.newArrayList();
            List < PadTaxInterest > listPad2 = ListUtils.newArrayList();
            for (PadTaxInterest padTaxInterest : listPad) {
                if ("1".equals(padTaxInterest.getStageType())) {
                    listPad1.add(padTaxInterest);
                } else if ("2".equals(padTaxInterest.getStageType())) {
                    listPad2.add(padTaxInterest);
                }
            }
            investorFinancialInfo.setPadTaxInterestList1(listPad1);
            investorFinancialInfo.setPadTaxInterestList2(listPad2);
        }
        List < Supplier > list = ListUtils.newArrayList();
        for (InvestorFinancialInfo info : investorFinancialInfoService.findList(new InvestorFinancialInfo())) {
            Supplier supplier = new Supplier();
            supplier.setSupplierId(info.getSupplier().getId());
            supplier.setSupplierName(info.getSupplier().getSupplierName());
            list.add(supplier);
        }
        List < String > names = ListUtils.newArrayList();
        List < Supplier > listSupplier = list.stream().filter(// 过滤去重
                v -> {
                    boolean flag = !names.contains(v.getSupplierId());
                    names.add(v.getSupplierId());
                    return flag;
                }
        ).collect(Collectors.toList());
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoFormPadTax";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "detailPadTax")
    public String detailPadTax(InvestorFinancialInfo investorFinancialInfo, Model model) {
        List < PadTaxInterest > listPad = investorFinancialInfo.getPadTaxInterestList();
        List < PadTaxInterest > listPad1 = ListUtils.newArrayList();
        List < PadTaxInterest > listPad2 = ListUtils.newArrayList();
        for (PadTaxInterest padTaxInterest : listPad) {
            if ("1".equals(padTaxInterest.getStageType())) {
                listPad1.add(padTaxInterest);
            } else if ("2".equals(padTaxInterest.getStageType())) {
                listPad2.add(padTaxInterest);
            }
        }
        investorFinancialInfo.setPadTaxInterestList1(listPad1);
        investorFinancialInfo.setPadTaxInterestList2(listPad2);

        Supplier supplier = new Supplier();
        supplier.setSupplierType(supplierTypeEnum.IN_SUPPLIER.getCode());// 国内资方
        List < Supplier > listSupplier = supplierService.findList(supplier);
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoDetailPadTax";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:edit")
    @PostMapping(value = "savePadTax")
    @ResponseBody
    @Transactional(readOnly = false)
    public String savePadTax(@Validated InvestorFinancialInfo investorFinancialInfo) {
        String padtaxType = investorFinancialInfo.getPadtaxType();
        InvestorFinancialInfo investorFinancialInfo1 = new InvestorFinancialInfo();
        investorFinancialInfo1.setGroupName(investorFinancialInfo.getGroupName());

        if (investorFinancialInfo.getIsNewRecord()) {
            List < InvestorFinancialInfo > listInfo = investorFinancialInfoService.findList(investorFinancialInfo1);
            if (listInfo != null && !listInfo.isEmpty()) {
                return renderResult(Global.FALSE, text("方案名称已存在！"));
            }
            investorFinancialInfo.setInvestorFinancialInfoId(codeRuleService.getCode("financialrateds"));
            investorFinancialInfo.setCreateBy(UserUtils.getUser().getUserCode());
            investorFinancialInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        } else {
            InvestorFinancialInfo oldInvestorFinancialInfo = investorFinancialInfoService.getOneByID(investorFinancialInfo.getInvestorFinancialInfoId());
            if (!investorFinancialInfo.getGroupName().equals(oldInvestorFinancialInfo.getGroupName())) {
                List < InvestorFinancialInfo > listInfo = investorFinancialInfoService.findList(investorFinancialInfo1);
                if (listInfo != null && !listInfo.isEmpty()) {
                    return renderResult(Global.FALSE, text("方案名称已存在！"));
                }
            }
            investorFinancialInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        }

        // 金融信息-垫税利息-关税保证保险1,现金2,舶来车3,国内融资4
        List < PadTaxInterest > listPad1 = investorFinancialInfo.getPadTaxInterestList1();
        List < PadTaxInterest > listPad2 = investorFinancialInfo.getPadTaxInterestList2();
        List < PadTaxInterest > listCheck = ListUtils.newArrayList();
        // 阶段赋值
        int supCode = 0;
        int CusCode = 0;
        for (PadTaxInterest padTaxInterest : listPad1) {
            if (!PadTaxInterest.STATUS_DELETE.equals(padTaxInterest.getStatus())) {
                padTaxInterest.setStage(supCode);
                supCode++;
                listCheck.add(padTaxInterest);
            }
            if ("1".equals(padtaxType)) {
                if ( padTaxInterest.getInvestorInterestAmount() == null){
                    return renderResult(Global.FALSE, text("资方利息金额不能为空！"));
                }
            }else {
                if ( padTaxInterest.getInvestorInterestAmount() == null){
                    padTaxInterest.setInvestorInterestAmount(0D);
                }
                if (StringUtils.isEmpty(padTaxInterest.getInvestorInterestRatio())){
                    return renderResult(Global.FALSE, text("资方年利率不能为空！"));
                }
            }
        }
        if (!CheckPadTax(listCheck)) {
            return renderResult(Global.FALSE, text("金融信息-垫税利息-客户的开始、结束天数必须连续！"));
        }
        listCheck.clear();
        for (PadTaxInterest padTaxInterest : listPad2) {
            if (!PadTaxInterest.STATUS_DELETE.equals(padTaxInterest.getStatus())) {
                padTaxInterest.setStage(CusCode);
                CusCode++;
                listCheck.add(padTaxInterest);
            }
            if ("1".equals(padtaxType)) {
                if (padTaxInterest.getCustomerInterestAmount() == null){
                    return renderResult(Global.FALSE, text("客户利息金额不能为空！"));
                }
            }else {
                if (padTaxInterest.getCustomerInterestAmount() == null){
                    padTaxInterest.setCustomerInterestAmount(0D);
                }
                if (StringUtils.isEmpty(padTaxInterest.getCustomerInterestRatio())){
                    return renderResult(Global.FALSE, text("客户年利率不能为空！"));
                }
            }
        }
        if (!CheckPadTax(listCheck)) {
            return renderResult(Global.FALSE, text("金融信息-垫税利息-客户的开始、结束天数必须连续！"));
        }
//        List < PadTaxInterest > listPad = investorFinancialInfo.getPadTaxInterestList();
        List < PadTaxInterest > listPad = ListUtils.newArrayList();
        listPad.addAll(listPad1);
        listPad.addAll(listPad2);
        investorFinancialInfo.setPadTaxInterestList(listPad);
        for (PadTaxInterest padTaxInterest : listPad) {
            padTaxInterest.setGroupName(investorFinancialInfo.getGroupName());
            padTaxInterest.setPadTaxInterestType(investorFinancialInfo.getPadtaxType());
        }
        investorFinancialInfoService.save(investorFinancialInfo);
        if (investorFinancialInfo.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("financialrateds");
        }
        return renderResult(Global.TRUE, text("保存资方金融费率维护成功！"));
    }


    /**
     * 查询列表
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = {"listInvFin", ""})
    public String listInvFin(InvestorFinancialInfo investorFinancialInfo, Model model) {
        List < Supplier > list = ListUtils.newArrayList();
        for (InvestorFinancialInfo info : investorFinancialInfoService.findList(new InvestorFinancialInfo())) {
            Supplier supplier = new Supplier();
            supplier.setSupplierId(info.getSupplier().getId());
            supplier.setSupplierName(info.getSupplier().getSupplierName());
            list.add(supplier);
        }
        List < String > names = ListUtils.newArrayList();
        List < Supplier > listSupplier = list.stream().filter(// 过滤去重
                v -> {
                    boolean flag = !names.contains(v.getSupplierId());
                    names.add(v.getSupplierId());
                    return flag;
                }
        ).collect(Collectors.toList());
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoListInvFin";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "listDataInvFin")
    @ResponseBody
    public Page < InvestorFinancialInfo > listDataInvFin(InvestorFinancialInfo investorFinancialInfo, HttpServletRequest request, HttpServletResponse response) {
        investorFinancialInfo.setPage(new Page <>(request, response));
        investorFinancialInfo.setFinancingType(InvestorFinanceInfoEnum.FINANCE.getCode()); // 融资
        Page < InvestorFinancialInfo > page = investorFinancialInfoService.findPage(investorFinancialInfo);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "formInvFin")
    public String formInvFin(InvestorFinancialInfo investorFinancialInfo, Model model) {
        if (!investorFinancialInfo.getIsNewRecord()) {
            List < PadTaxInterest > listPad = investorFinancialInfo.getPadTaxInterestList();
            List < PadTaxInterest > listPad3 = ListUtils.newArrayList();
            List < PadTaxInterest > listPad4 = ListUtils.newArrayList();
            for (PadTaxInterest padTaxInterest : listPad) {
                if ("1".equals(padTaxInterest.getStageType())) {
                    listPad3.add(padTaxInterest);
                } else if ("2".equals(padTaxInterest.getStageType())) {
                    listPad4.add(padTaxInterest);
                }
            }
            investorFinancialInfo.setPadTaxInterestList3(listPad3);
            investorFinancialInfo.setPadTaxInterestList4(listPad4);
        }
        List < Supplier > list = ListUtils.newArrayList();
        for (InvestorFinancialInfo info : investorFinancialInfoService.findList(new InvestorFinancialInfo())) {
            Supplier supplier = new Supplier();
            supplier.setSupplierId(info.getSupplier().getId());
            supplier.setSupplierName(info.getSupplier().getSupplierName());
            list.add(supplier);
        }
        List < String > names = ListUtils.newArrayList();
        List < Supplier > listSupplier = list.stream().filter(// 过滤去重
                v -> {
                    boolean flag = !names.contains(v.getSupplierId());
                    names.add(v.getSupplierId());
                    return flag;
                }
        ).collect(Collectors.toList());
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoFormInvFin";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:view")
    @RequestMapping(value = "detailInvFin")
    public String detailInvFin(InvestorFinancialInfo investorFinancialInfo, Model model) {
        List < PadTaxInterest > listPad = investorFinancialInfo.getPadTaxInterestList();
        List < PadTaxInterest > listPad3 = ListUtils.newArrayList();
        List < PadTaxInterest > listPad4 = ListUtils.newArrayList();
        for (PadTaxInterest padTaxInterest : listPad) {
            if ("1".equals(padTaxInterest.getStageType())) {
                listPad3.add(padTaxInterest);
            } else if ("2".equals(padTaxInterest.getStageType())) {
                listPad4.add(padTaxInterest);
            }
        }
        investorFinancialInfo.setPadTaxInterestList3(listPad3);
        investorFinancialInfo.setPadTaxInterestList4(listPad4);

        Supplier supplier = new Supplier();
        supplier.setSupplierType(supplierTypeEnum.IN_SUPPLIER.getCode());// 国内资方
        List < Supplier > listSupplier = supplierService.findList(supplier);
        model.addAttribute("listSupplier", listSupplier);
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("investorFinancialInfo", investorFinancialInfo);
        return "modules/base/investorfinancialinfo/investorFinancialInfoDetailInvFin";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("base:investorfinancialinfo:investorFinancialInfo:edit")
    @PostMapping(value = "saveInvFin")
    @ResponseBody
    @Transactional(readOnly = false)
    public String saveInvFin(@Validated InvestorFinancialInfo investorFinancialInfo) {
        InvestorFinancialInfo investorFinancialInfo1 = new InvestorFinancialInfo();
        investorFinancialInfo1.setGroupName(investorFinancialInfo.getGroupName());

        if (investorFinancialInfo.getIsNewRecord()) {
            List < InvestorFinancialInfo > listInfo = investorFinancialInfoService.findList(investorFinancialInfo1);
            if (listInfo != null && !listInfo.isEmpty()) {
                return renderResult(Global.FALSE, text("方案名称已存在！"));
            }
            investorFinancialInfo.setInvestorFinancialInfoId(codeRuleService.getCode("financialraterz"));
            investorFinancialInfo.setCreateBy(UserUtils.getUser().getUserCode());
            investorFinancialInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        } else {
            InvestorFinancialInfo oldInvestorFinancialInfo = investorFinancialInfoService.getOneByID(investorFinancialInfo.getInvestorFinancialInfoId());
            if (!investorFinancialInfo.getGroupName().equals(oldInvestorFinancialInfo.getGroupName())) {
                List < InvestorFinancialInfo > listInfo = investorFinancialInfoService.findList(investorFinancialInfo1);
                if (listInfo != null && !listInfo.isEmpty()) {
                    return renderResult(Global.FALSE, text("方案名称已存在！"));
                }
            }
            investorFinancialInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        }

        List < PadTaxInterest > listPad3 = investorFinancialInfo.getPadTaxInterestList3();
        List < PadTaxInterest > listPad4 = investorFinancialInfo.getPadTaxInterestList4();
        List < PadTaxInterest > listCheck = ListUtils.newArrayList();
        // 阶段赋值
        int supCode = 1;
        int CusCode = 1;
        for (PadTaxInterest padTaxInterest : listPad3) {
            if (!PadTaxInterest.STATUS_DELETE.equals(padTaxInterest.getStatus())) {
                padTaxInterest.setStage(supCode);
                supCode++;
                listCheck.add(padTaxInterest);
            }
        }
        if (!CheckPadTax(listCheck)) {
            return renderResult(Global.FALSE, text("金融信息-垫税利息-客户的开始、结束天数必须连续！"));
        }
        listCheck.clear();
        for (PadTaxInterest padTaxInterest : listPad4) {
            if (!PadTaxInterest.STATUS_DELETE.equals(padTaxInterest.getStatus())) {
                padTaxInterest.setStage(CusCode);
                CusCode++;
                listCheck.add(padTaxInterest);
            }
        }
        if (!CheckPadTax(listCheck)) {
            return renderResult(Global.FALSE, text("金融信息-垫税利息-客户的开始、结束天数必须连续！"));
        }
        List < PadTaxInterest > listPad = investorFinancialInfo.getPadTaxInterestList();
        listPad.addAll(listPad3);
        listPad.addAll(listPad4);
        investorFinancialInfo.setPadTaxInterestList(listPad);
        for (PadTaxInterest padTaxInterest : listPad) {
            padTaxInterest.setGroupName(investorFinancialInfo.getGroupName());
            padTaxInterest.setFinancingMethod(investorFinancialInfo.getFinancingMethod());
        }
        investorFinancialInfoService.save(investorFinancialInfo);
        if (investorFinancialInfo.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("financialraterz");
        }
        return renderResult(Global.TRUE, text("保存资方金融费率维护成功！"));
    }


}